package com.xiaodu.springboot.controller;

import com.xiaodu.springboot.entity.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: dutianjie
 * @date: 2021/4/7
 * @version: v1.0.0
 * @description: 描述
 **/
@RestController
@RequestMapping("ex")
@Api(tags = {"测试局部异常处理"})
public class TestPartExceptionContorller {

    @GetMapping("ex3")
    @ApiOperation(value = "测试局部异常处理", httpMethod = "GET")
    public void testPartException() throws Exception {
        int i = 1 / 0; // 模拟程序报错
    }

    /*局部异常处理，只处理当前controller的异常信息*/
    @ExceptionHandler(value = {Exception.class})
    public ResponseEntity<ResultBean<Void>> handlerPartException(HttpServletRequest request, HttpServletResponse response, Exception e) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ResultBean.result(HttpStatus.INTERNAL_SERVER_ERROR));
    }

}
